package com.stylefeng.guns.modular.payroll.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.payroll.MapToPayroll;
import com.stylefeng.guns.modular.payroll.model.PayrollImportHandler;
import com.stylefeng.guns.modular.system.model.Payroll;
import com.stylefeng.guns.modular.system.dao.PayrollMapper;
import com.stylefeng.guns.modular.payroll.service.IPayrollService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-09
 */
@Service
public class PayrollServiceImpl extends ServiceImpl<PayrollMapper, Payroll> implements IPayrollService {

    /**
     * 工资表导入
     * @param file
     * @throws Exception
     * @author laoyu
     * @Date 2018-07-24
     */
    @Override
    public void adds(MultipartFile file) throws Exception {
        try{
            ImportParams params = new ImportParams();
            params.setTitleRows(1);//标题所占行数
            params.setHeadRows(4);//表头所占行数
            params.setDataHandler(new PayrollImportHandler());
            //解析表格中的数据
            List<Map<String, Object>> list   = ExcelImportUtil.importExcel(
                    file.getInputStream(), Map.class, params);
            //获取标题中的工资日期
            String fileName = file.getOriginalFilename();
            //获取要上传的表格中的报表统计时间
            String payTime = fileName.substring(fileName.length()-15,fileName.length()-8);
            //将表格数据转化成工资表对应的实体类
            List<Payroll> payrollList = MapToPayroll.mapToPayroll(list,payTime);
            //检查是否存在当月数据
            String platform = ShiroKit.getUser().getDeptcode();
            if (!platform.equals(payrollList.get(0).getPlatform())){
                throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"请检查要上传的工资表是否为本平台的员工工资表");
            }
            EntityWrapper<Payroll> wrapper  = new EntityWrapper<>();
            wrapper.eq("platform",platform);
            wrapper.eq("pay_time",payTime);
            List<Payroll> payrolls = this.selectList(wrapper);
            if (!payrolls.isEmpty()){
                throw new GunsException(BizExceptionEnum.BUSINSS_ERROR,"当月工资表已经存在，不允许导入");
            }
            //将表数据插入数据库
            this.insertBatch(payrollList);
        }catch (Exception ex){
            ex.printStackTrace();
            throw ex;
        }

    }


    /**
     * 组装要下载的工资标识数据
     * @param list
     * @return
     * @author laoyu
     * @Date 2018-07-24
     */

    @Override
    public List<Map<String, Object>> load(List<Payroll> list) {
        List<Map<String, Object>> payrollList = new ArrayList<Map<String, Object>>();
        for (Payroll payroll:list) {
            Map<String, Object> payrollMap = new HashMap<String, Object>();
            payrollMap.put("usercode", payroll.getUsercode());
            payrollMap.put("username", payroll.getUsername() );
            payrollMap.put("platform", payroll.getPlatform());
            payrollMap.put("entryTime",payroll.getEntryTime());
            payrollMap.put("basicSalary", payroll.getBasicSalary());
            payrollMap.put("bonus", payroll.getBonus());
            payrollMap.put("attendanceDays", payroll.getAttendanceDays());
            payrollMap.put("actualBasicSalary", payroll.getActualBasicSalary());
            payrollMap.put("actualBonus",payroll.getActualBonus());
            payrollMap.put("performanceBonus",payroll.getPerformanceBonus());
            payrollMap.put("commission",payroll.getCommission());
            payrollMap.put("norepairBonus",payroll.getNorepairBonus());
            payrollMap.put("otherSubsidies",payroll.getOtherSubsidies());
            payrollMap.put("leaveDays",payroll.getLeaveDays());
            payrollMap.put("leaveDeduction",payroll.getLeaveDeduction());
            payrollMap.put("lateArrivals",payroll.getLateArrivals());
            payrollMap.put("letaDeduction",payroll.getLetaDeduction());
            payrollMap.put("otherDeduction",payroll.getOtherDeduction());
            payrollMap.put("advancePiso",payroll.getAdvancePiso());
            payrollMap.put("advanceRmb",payroll.getAdvanceRmb());
            payrollMap.put("realRmb",payroll.getRealRmb());
            payrollMap.put("sendPiso",payroll.getSendPsio());
            payrollMap.put("roomSupplement",payroll.getRoomSupplement());
            payrollMap.put("realPiso",payroll.getRealPiso());
            payrollMap.put("remarks",payroll.getRemarks());


            payrollList.add(payrollMap);
        }
        return payrollList;
    }
}
